package com.xywang.controller;

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import com.xywang.dao.bill.BillDao;
import com.xywang.pojo.Provider;
import com.xywang.pojo.User;
import com.xywang.service.bill.BillService;
import com.xywang.service.provider.ProviderService;
import com.xywang.service.provider.ProviderServiceImpl;
import com.xywang.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/jsp")
public class ProviderController {
    private ProviderService providerServiceImpl;

    @Autowired
    public void setProviderServiceImpl(ProviderService providerServiceImpl) {
        this.providerServiceImpl = providerServiceImpl;
    }
    @RequestMapping("/getProviderList")
    @ResponseBody
    public String getProviderList(){
        return JSONArray.toJSONString(providerServiceImpl.getProviderListService("",""));
    }
    // 从左侧导航栏点击进入供应商管理页面，展示列表，首次进入无参数，之后查询使用同一请求链接
    // 携带参数，实现条件查询展示列表
    @RequestMapping("/providerQuery")
    public String providerQuery(String queryProCode, String queryProName, Model model){
        if (StringUtils.isNullOrEmpty(queryProName)) {
            queryProName = "";
        }
        if (StringUtils.isNullOrEmpty(queryProCode)) {
            queryProCode = "";
        }
        // 获取供应商列表，输出到前端
        List<Provider> providerList = providerServiceImpl.getProviderListService(queryProCode, queryProName);
        // 像前端发送数据

        model.addAttribute("providerList", providerList);
        model.addAttribute("queryProCode", queryProCode);
        model.addAttribute("queryProName", queryProName);
        return "providerList";
    }
    // 跳转到供应商添加页面
    @RequestMapping("/toProviderAddPage")
    public String toProviderAddPage(){
        return "providerAdd";
    }
    // 以下三个请求来自js文件
    @RequestMapping("/delProvider")
    @ResponseBody
    public String delProvider(String proid){
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if (!StringUtils.isNullOrEmpty(proid)) {
            // 如果有订单的供应商是供应商id号为proid的，则该供应商不能删除
            int billCount = providerServiceImpl.deleteProviderByIdService(Integer.parseInt(proid));
            if (billCount == 0) {//删除成功
                resultMap.put("delResult", "true");
            } else if (billCount == -1) {//删除失败
                resultMap.put("delResult", "false");
            } else if (billCount > 0) {//该供应商下有订单，不能删除，返回订单数
                resultMap.put("delResult", String.valueOf(billCount));
            }
        } else {
            resultMap.put("delResult", "notexit");
        }
        return JSONArray.toJSONString(resultMap);
    }
    @RequestMapping("/providerView")
    public String providerView(String proid,Model model){
        if (!StringUtils.isNullOrEmpty(proid)) {
            //调用后台方法得到provider对象
            Provider provider = providerServiceImpl.getProviderByIdService(Integer.parseInt(proid));
            model.addAttribute("provider", provider);
            return "providerView";
        }else{
            return "../../error";
        }
    }
    @RequestMapping("/providerModify")
    public String providerModify(String proid,Model model){
        if (!StringUtils.isNullOrEmpty(proid)) {
            //调用后台方法得到provider对象
            Provider provider = providerServiceImpl.getProviderByIdService(Integer.parseInt(proid));
            model.addAttribute("provider", provider);
            return "providerModify";
        }else{
            return "../../error";
        }
    }
    @RequestMapping("/providerAdd")
    public String providerAdd(Provider provider, HttpSession session){
        provider.setCreatedBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
        provider.setCreationDate(new Date());
        if(providerServiceImpl.addProService(provider)){
            return "redirect:/jsp/providerQuery";
        }else{
            return "providerAdd";
        }
    }
    @RequestMapping("/providerModifySave")
    public String modifySave(Provider provider,HttpSession session){
        provider.setModifyBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
        provider.setModifyDate(new Date());
        if(providerServiceImpl.modifyService(provider)){
            return "redirect:/jsp/providerQuery";
        }else{
            return "providerModify";
        }
    }
}
